package com.ysd.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.exception.RRException;
import com.ysd.common.utils.DateUtils;
import com.ysd.common.utils.SensitivewordFilter;
import com.ysd.modules.app.dao.NoteReplyDao;
import com.ysd.modules.app.dao.StudentDao;
import com.ysd.modules.app.dao.StudentNoteDao;
import com.ysd.modules.app.dao.TopicDao;
import com.ysd.modules.app.entity.NoteReply;
import com.ysd.modules.app.entity.StudentEntity;
import com.ysd.modules.app.entity.StudentNote;
import com.ysd.modules.app.entity.TopicEntity;
import com.ysd.modules.app.service.StudentNoteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class StudentNoteServiceImpl extends ServiceImpl<StudentNoteDao, StudentNote> implements StudentNoteService {
    @Resource
    private NoteReplyDao noteReplyDao;
    @Resource
    private StudentDao studentDao;
    @Resource
    private TopicDao topicDao;

    /**
     * 添加学生个人笔记
     *
     * @param stuId
     * @param topicId
     * @param content
     * @return
     */
    @Override
    public int insert(Integer stuId, Integer topicId, String content) {
        StudentNote studentNote = new StudentNote();
        StudentEntity studentEntity = studentDao.selectById(stuId);
        if (studentEntity == null){
            throw new RRException("没有该学生");
        }
        studentNote.setStuId(stuId);
        studentNote.setStuName(studentEntity.getName());
        studentNote.setCreatedTime(DateUtils.format(new Date()));
        TopicEntity topicEntity =  topicDao.selectById(topicId);
        if (topicEntity == null){
            throw new RRException("没有该题目");
        }
        studentNote.setTopicId(topicId);
        studentNote.setTopicName(topicEntity.getTitle());

        /**
         * 敏感词屏蔽
         */
        SensitivewordFilter filter = new SensitivewordFilter();
        content = filter.replaceSensitiveWord(content, 2,"*");
        System.out.println("语句中包含敏感词的个数为：" + content);
        studentNote.setContent(content);
        studentNote.setStatus(0);
        return baseMapper.insert(studentNote);
    }

    /**
     * 增加回复
     *
     * @param noteReply
     * @return
     */
    @Override
    public int addReply(NoteReply noteReply) {

        return noteReplyDao.insert(noteReply);
    }

    /**
     * 查询所有的笔记以及回复
     *
     * @param topicId 题目id
     * @return
     */
    @Override
    public List<StudentNote> studentNoteList(Integer topicId) {
        QueryWrapper<StudentNote> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("topic_id", topicId);
        queryWrapper.eq("status", 0);
        List<StudentNote> studentNoteList = baseMapper.selectList(queryWrapper);
        if (studentNoteList != null && studentNoteList.size() > 0) {
            for (int i = 0; i < studentNoteList.size(); i++) {
                StudentNote studentNote = studentNoteList.get(i);
                QueryWrapper<NoteReply> nr = new QueryWrapper<>();
                queryWrapper.eq("note_id", studentNote.getId());
                queryWrapper.eq("status", 0);
                List<NoteReply> noteReplyList = noteReplyDao.selectList(nr);
                studentNote.setNoteReplyList(noteReplyList);
            }
        }
        return studentNoteList;
    }

    /**
     * 查询我的笔记以及回复
     *
     * @param stuId
     * @return
     */
    @Override
    public List<StudentNote> myStudentNoteList(Integer stuId) {
        QueryWrapper<StudentNote> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_id", stuId);
        queryWrapper.eq("status", 0);
        List<StudentNote> studentNoteList = baseMapper.selectList(queryWrapper);
        if (studentNoteList != null && studentNoteList.size() > 0) {
            for (int i = 0; i < studentNoteList.size(); i++) {
                StudentNote studentNote = studentNoteList.get(i);
                QueryWrapper<NoteReply> nr = new QueryWrapper<>();
                queryWrapper.eq("note_id", studentNote.getId());
                queryWrapper.eq("status", 0);
                List<NoteReply> noteReplyList = noteReplyDao.selectList(nr);
                studentNote.setNoteReplyList(noteReplyList);
            }
        }
        return studentNoteList;
    }

    /**
     * 删除笔记
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer id) {
        return baseMapper.deleteById(id);
    }
}
